package com.example.big_orange2.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.pagedata_m;
import com.example.big_orange2.entity.sharelock;
import com.example.big_orange2.mapper.sharelockMapper;
import javafx.util.Pair;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShareLockService {
    @Resource
    sharelockMapper sharelockMapper;

    public Result<?> findbysum(){
        LambdaQueryWrapper<sharelock> wrapper1 = Wrappers.<sharelock>lambdaQuery();
        wrapper1.eq(sharelock::getSharelockstatus,"Available");//多条件查询
        Integer useNum=sharelockMapper.selectCount(wrapper1);
        LambdaQueryWrapper<sharelock> wrapper2 =Wrappers.<sharelock>lambdaQuery();
        wrapper2.eq(sharelock::getSharelockstatus,"Isuse");//多条件查询
        Integer avaibleUseNum=sharelockMapper.selectCount(wrapper2);
        LambdaQueryWrapper<sharelock> wrapper3 =Wrappers.<sharelock>lambdaQuery();
        Integer totalUseNum=sharelockMapper.selectCount(wrapper3);
        List<Pair<String,Integer>> Loanhandle=new ArrayList<Pair<String,Integer>>();
        Pair<String,Integer> s1=new Pair("avaibleUseNum",avaibleUseNum);
        Loanhandle.add(s1);
        s1=new Pair("useNum",useNum);
        Loanhandle.add(s1);
        s1=new Pair("totalNum",totalUseNum);
        Loanhandle.add(s1);
        return  Result.success(Loanhandle);//返回结果
    }

    //分页查询(select),查询数据接口
    public Result<?> findPage(Integer page, Integer limit, String s){
        pagedata_m ru=new pagedata_m();
        LambdaQueryWrapper<sharelock> wrapper =Wrappers.<sharelock>lambdaQuery();
        if(StrUtil.isNotBlank(s)){//判断search不是空
            wrapper.like(com.example.big_orange2.entity.sharelock::getSharelockid,Integer.parseInt(s));

        }
        Page<sharelock> ClientPage=sharelockMapper.selectPage(new Page<>(page,limit),wrapper);
        //?是泛型，表示任意类型;pageNum:前台第几页；pageSize：前台每页多少条数据；search：前台要搜索的内容;defaltvalue:默认值
        //new Page<>(pageNum,pageSize);     MybatisPlusConfig提供的分页对象。
        //Client::四个点可以直接访问对象的属性; like模糊查询;
        ru.setSharelockPage(ClientPage);
        ru.setTotal(ClientPage.getTotal());
        return  Result.success(ru);//返回结果

    }
}
